/**
 * AjLog - The AspectJ based logging facade
 *
 * Copyright (C) 2009 - 2010, Marko Umek. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajlog.core.logging;

import org.ajlog.util.AjLogAssert;

/**
 * AbstractLogMethod contains the base implementation which should be used by
 * any logging framework implementation.
 * 
 * @author Marko Umek
 * 
 */
public abstract class AbstractLogMethod<LOGGER extends Object>
    implements ILogMethod {

    protected LOGGER logger = null;

    /**
     * Constructor
     * 
     */
    public AbstractLogMethod() {
    }

    /* (non-Javadoc)
     * @see org.ajlog.core.logging.ILogMethod#log(org.ajlog.core.logging.ILogEntry)
     */
    public final void log(final ILogEntry logEntry) {
        final Throwable exception = logEntry.getException();
        if (exception == null) {
            this.doLogWithoutException(logEntry);
        }
        else {
            this.doLogWithException(logEntry, exception);
        }

    }

    /**
     * TODO: doLogWithException is responsible for ...
     * 
     * @param logEntry
     * @param exception
     */
    protected void doLogWithException(final ILogEntry logEntry, final Throwable exception) {
        this.doLogWithoutException(logEntry);
    }

    /**
     * TODO: doLogWithoutException is responsible for ...
     * 
     * @param logEntry
     */
    protected abstract void doLogWithoutException(final ILogEntry logEntry);

    /**
     * Set the logging instance
     */
    @SuppressWarnings("unchecked")
    private final void setLogger(final Object logger) {
        try {
            this.logger = (LOGGER) logger;
        } catch (final ClassCastException ex) {
            AjLogAssert.fail("Class cast exception: " + logger.getClass() + " is not assignable to current logger instance.");
        }
    }

    /* (non-Javadoc)
     * @see org.ajlog.core.logging.ILogMethod#isValid()
     */
    public final boolean isValid() {
        return this.logger != null;
    }

    /* (non-Javadoc)
     * @see org.ajlog.core.logging.ILogMethod#createCopy(java.lang.Object)
     */
    @SuppressWarnings("unchecked")
    public final ILogMethod createCopy(final Object loggingInstance) {
        AbstractLogMethod<LOGGER> copy = null;
        try {
            copy = (AbstractLogMethod<LOGGER>) this.clone();
            copy.setLogger(loggingInstance);
        } catch (final CloneNotSupportedException ex) {
            AjLogAssert.fail("CloneNotSupportedException: Should never happen, cause ILogMethod already extends Cloneable.");
        }
        AjLogAssert.notNull(copy, "Panic. the copy must never be null.");
        if (!copy.isValid()) {
            AjLogAssert.fail("Panic. the copies logger instance must never be null.");
        }
        return copy;
    }
}
